package neu.soft.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import neu.soft.constant.GoodSaleType;
import neu.soft.entity.Good;
import neu.soft.exception.AppException;
import neu.soft.exception.AppExceptionCodeMsg;
import neu.soft.mapper.GoodsMapper;
import neu.soft.query.BasePageQuery;
import neu.soft.service.GoodsService;
import neu.soft.util.ChineseCharacterUtils;
import neu.soft.util.TypeConvert;
import neu.soft.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public int addGood(Good good) {

        int num = goodsMapper.selectGood(good);
        if (num!=0){
            throw new AppException(AppExceptionCodeMsg.REPEAT_ADD);
        }
        String goodType = good.getGoodType();
        String measure = good.getMeasure();
        String vendor = good.getVendor();
        String goodName = good.getGoodName();
        Integer typeId = goodsMapper.findTypeCode(goodType);//获取商品分类的代码
        String measureCode = goodsMapper.findMeasureCode(measure);//获取商品计量单位的代码
        StringBuilder stringBuilder = new StringBuilder("G690");//商品代码默认标识
        String goodNamePinyin = String.valueOf(ChineseCharacterUtils.getNameFormat(goodName,goodName.length()));
        stringBuilder.append(goodNamePinyin);
        stringBuilder.append(typeId);
        stringBuilder.append(measureCode);
        String vendorInfo = String.valueOf(ChineseCharacterUtils.getPinYinForGood(vendor));
        stringBuilder.append(vendorInfo.hashCode());
        good.setGoodId(String.valueOf(stringBuilder));
        stringBuilder.delete(0, stringBuilder.length());
        stringBuilder = null;
        int res = goodsMapper.addGood(good);
        if (res < 1) {
            throw new AppException(AppExceptionCodeMsg.DZYJ_NGDZ);
        }
        return res;
    }


    @Override
    public int updateGood(Good good) {

        int num = goodsMapper.selectGood(good);
        if (num == 1){
            throw new AppException(AppExceptionCodeMsg.REPEAT_UPDATE);
        }
        int res = goodsMapper.updateGood(good);
        if (res < 1) {
            throw new AppException(AppExceptionCodeMsg.YYDZ_JDWGXSB);
        }
        return res;

    }

    @SneakyThrows
    @Override
    public int deleteGood(String goodId) {

        int res = goodsMapper.deleteGood(goodId);
        if (res < 1) {
            throw new AppException(AppExceptionCodeMsg.NSRAD_JSNDSH);
        }
        return res;
    }

    @Override
    public List<Good> findAll() {
        return goodsMapper.findAll();
    }

    @Override
    public Good findById(String goodId) {
        return goodsMapper.findById(goodId);
    }

    @Override
    public PageBean<Good> findByPage(BasePageQuery query) {

        PageBean<Good> pageBean = new PageBean<>();
        PageHelper.startPage(query.getPageNow(),query.getPageSize());
        List<Good> goodList = goodsMapper.findByPage(query);
        goodList.forEach(good -> {
            good.setIsReplacement(TypeConvert.enumToString(GoodSaleType.class,good.getIsReplacement()));
            good.setIsReturn(TypeConvert.enumToString(GoodSaleType.class,good.getIsReturn()));
        });
        PageInfo<Good> pageInfo = new PageInfo<>(goodList);
        pageBean.setTotalRows(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setRowData(pageInfo.getList());
        return pageBean;
    }

    @Override
    public Map<String,List<String>> findAllInfo() {
        Map<String,List<String>> res = new HashMap<>();
        List<String> typeList = goodsMapper.findAllGoodType();
        List<String> measureList = goodsMapper.findAllMeasure();
        res.put("type",typeList);
        res.put("measure",measureList);
        return res;
    }



    @Override
    public List<Good> batchFind(List<String> code) {
        List<Good> goodList = goodsMapper.batchFindGood(code);
        return goodList;
    }
}
